package frame;

import bridge.ImpLog;
import bridge.JImpLog;
import bridge.Log;
import bridge.TextFileLog;
import emp.EmpGoods;
import mapper.GoodsMapper;
import mapper.UserMapper;
import observer.ComputeObserver;
import observer.ConcreteSubject;
import observer.Observer;
import observer.Subject;
import org.apache.ibatis.session.SqlSession;
import pojo.Goods;
import utils.MybatisUtil;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

//import com.system.bean.Goods;
//import com.system.bean.User;
//import com.system.dao.GoodsmanagementImp;
//import com.system.dao.StoragemanagementImp;

import java.awt.Color;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;
import java.sql.SQLException;

import javax.swing.SwingConstants;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.List;

public class InputFrame extends MainFrame {
    private JPanel contentPane;
    private JTextField nameField;
    private JTextField shelfField;
    private JTextField numberField;
    private JTextField priceField;

    private JTextField jTextField;
    private JTextField calField;

    // 创建一个抽象被观察者的实例
    private Subject subject = new ConcreteSubject();

    // 每次点击添加时去创建一个新的
    private Observer observer;

    // 第一次初始化，以后直接clone
    private Goods headGoods = new Goods();
    private List<Goods> goodsArrayList = new ArrayList<>();
    private SqlSession sqlSession = MybatisUtil.getSession();
    // 创建userDao接口，然后通过反射来获取代理对象
    private GoodsMapper goodsMapper = sqlSession.getMapper(GoodsMapper.class);

    public InputFrame()  {
        //货物信息
        setLocationRelativeTo(null);
        JPanel panel = new JPanel();
        panel.setBackground(Color.WHITE);
        panel.setBounds(141, 36, 637, 476);//width:637
        getContentPane().add(panel);
        panel.setLayout(null);

        /*//进货清单
        JPanel panel2 = new JPanel();
        panel.setBackground(Color.WHITE);
        panel.setBounds(778, 36, 318, 476);
        getContentPane().add(panel);
        panel.setLayout(null);*/

        JLabel lblNewLabel = new JLabel("货物信息");
        lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
        lblNewLabel.setForeground(Color.BLUE);
        lblNewLabel.setFont(new Font("微软雅黑", Font.BOLD, 18));
        lblNewLabel.setBounds(154, 46, 229, 36);//x:154
        panel.add(lblNewLabel);

        /*JLabel lblNewLabel2 = new JLabel("进货清单");
        lblNewLabel2.setHorizontalAlignment(SwingConstants.CENTER);
        lblNewLabel2.setForeground(Color.BLUE);
        lblNewLabel2.setFont(new Font("微软雅黑", Font.BOLD, 18));
        lblNewLabel2.setBounds(68, 46, 229, 36);//x:154
        panel2.add(lblNewLabel2);*/

        JLabel lblNewLabel_1 = new JLabel("货物名称：");
        lblNewLabel_1.setHorizontalAlignment(SwingConstants.CENTER);
        lblNewLabel_1.setFont(new Font("微软雅黑", Font.BOLD, 16));
        lblNewLabel_1.setBounds(78, 104, 96, 27);
        panel.add(lblNewLabel_1);

        JLabel lblNewLabel_1_1 = new JLabel("货架号：");
        lblNewLabel_1_1.setHorizontalAlignment(SwingConstants.CENTER);
        lblNewLabel_1_1.setFont(new Font("微软雅黑", Font.BOLD, 16));
        lblNewLabel_1_1.setBounds(78, 153, 96, 27);
        panel.add(lblNewLabel_1_1);

        JLabel lblNewLabel_1_2 = new JLabel("货物数量：");
        lblNewLabel_1_2.setHorizontalAlignment(SwingConstants.CENTER);
        lblNewLabel_1_2.setFont(new Font("微软雅黑", Font.BOLD, 16));
        lblNewLabel_1_2.setBounds(78, 199, 96, 27);
        panel.add(lblNewLabel_1_2);

        JLabel lblNewLabel_1_3 = new JLabel("货物单价：");
        lblNewLabel_1_3.setHorizontalAlignment(SwingConstants.CENTER);
        lblNewLabel_1_3.setFont(new Font("微软雅黑", Font.BOLD, 16));
        lblNewLabel_1_3.setBounds(78, 255, 96, 27);
        panel.add(lblNewLabel_1_3);

        JLabel lblNewLabel_1_4 = new JLabel("总计：");
        lblNewLabel_1_4.setHorizontalAlignment(SwingConstants.CENTER);
        lblNewLabel_1_4.setFont(new Font("微软雅黑", Font.BOLD, 12));
        lblNewLabel_1_4.setBounds(78, 440, 50, 14);
        panel.add(lblNewLabel_1_4);

        nameField = new JTextField();
        nameField.setBounds(164, 103, 229, 31);
        panel.add(nameField);
        nameField.setColumns(10);

        shelfField = new JTextField();
        shelfField.setColumns(10);
        shelfField.setBounds(164, 157, 229, 31);
        panel.add(shelfField);

        numberField = new JTextField();
        numberField.setColumns(10);
        numberField.setBounds(164, 203, 229, 31);
        panel.add(numberField);

        priceField = new JTextField();
        priceField.setColumns(10);
        priceField.setBounds(164, 257, 229, 31);
        panel.add(priceField);

        calField = new JTextField();
        calField.setColumns(10);
        calField.setBounds(130, 438, 100, 20);
        panel.add(calField);


        /*StoragemanagementImp s = new StoragemanagementImp();
        try {
            s.Query1("select storageID from storage");
            //id = new JComboBox(StoragemanagementImp.vr);
        } catch (SQLException e2) {
            // TODO 自动生成的 catch 块
            e2.printStackTrace();
        }
        comboBox = new JComboBox(StoragemanagementImp.vr);
        comboBox.setBounds(164, 251, 229, 36);
        panel.add(comboBox);*/
        jTextField = new JTextField();
        jTextField.setColumns(10);
        jTextField.setBounds(78, 340, 388, 92);
        panel.add(jTextField);


        JButton btnNewButton = new JButton("提交");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Goods goods = new Goods();
                if(nameField.getText().equals("") || shelfField.getText().equals("") || numberField.getText().equals("")) {
                    JOptionPane.showMessageDialog(null,"请输入内容");
                } else {
                    Goods curGoods = (Goods) headGoods.clone();
                    curGoods.setgName(nameField.getText());
                    curGoods.setgShelf(shelfField.getText());
                    curGoods.setgCnt(Integer.parseInt(numberField.getText()));
                    curGoods.setgPrice(Double.parseDouble(priceField.getText()));
                    goodsArrayList.add(curGoods);
                    try {
                        // 使用观察者更新清单和总价格
                        observer = new ComputeObserver((ArrayList<Goods>) goodsArrayList);
                        // 添加观察者
                        subject.attach(observer);
                        // 通知观察者更新
                        EmpGoods ans = subject.transform();
                        // 将结果显示在列表上
                        jTextField.setText(ans.getMessage());
                        calField.setText("￥" + (Math.round(ans.getTolPrice() * 100) / 100.0));
                        // 注销当前对象
                        subject.detach(observer);

                        // 清空表单数据
                        nameField.setText("");
                        shelfField.setText("");
                        numberField.setText("");
                        priceField.setText("");
                        JOptionPane.showMessageDialog(null, "提交成功");
                    } catch (Exception e1) {
                        // TODO 自动生成的 catch 块
                        e1.printStackTrace();
                    }
                }
            }
        });
        btnNewButton.setFont(new Font("微软雅黑", Font.BOLD, 14));
        btnNewButton.setBounds(78, 300, 70, 30);
        panel.add(btnNewButton);

        JButton btnNewButton_1 = new JButton("重置");
        btnNewButton_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                nameField.setText("");
                shelfField.setText("");
                numberField.setText("");
                priceField.setText("");
            }
        });
        btnNewButton_1.setFont(new Font("微软雅黑", Font.BOLD, 14));
        btnNewButton_1.setBounds(237, 300, 70, 30);
        panel.add(btnNewButton_1);

        JButton btnNewButton_2 = new JButton("入库");
        btnNewButton_2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                // 将所有商品信息都插入到数据库中
                int cnt = goodsMapper.addGoods(goodsArrayList);
                // 注意提交事务，不然没有增加数据
                if (cnt == goodsArrayList.size()) {
                    sqlSession.commit();
                    // 打印日志
            /*IPrintLog printLog = new PrintLogSon();
            PrintLogProxy proxy = new PrintLogProxy(printLog);
            proxy.output(textArea.getText());*/

                    ImpLog j = new JImpLog(); //建立java平台
                    Log jl = new TextFileLog(j); //建立基于java 的文本文件日志写入
                    jl.write(jTextField.getText()); //写入日志文件

                    JOptionPane.showMessageDialog(null, "入库成功！日志已成功写入！");
                    calField.setText("￥0.00");
                    jTextField.setText("");
                } else {
                    JOptionPane.showMessageDialog(null, "入库失败！");
                }
            }
        });
        btnNewButton_2.setFont(new Font("微软雅黑", Font.BOLD, 14));
        btnNewButton_2.setBounds(396, 300, 70, 30);
        panel.add(btnNewButton_2);



    }
}
